package com.edais.service.imp.gift;

import com.edais.domain.gift.PointAccount;
import com.edais.domain.gift.PointOrder;
import com.edais.persistence.gift.PointOrderMapper;
import com.edais.service.UserService;
import com.edais.service.gift.PointOrderService;
import com.edais.util.Consts;
import com.edais.util.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * PointOrderService
 * Created by haoyx on 2016/10/8.
 */
@Service
public class PointOrderServiceImpl implements PointOrderService {

    private static final Logger logger = Logger.getLogger(PointOrderServiceImpl.class);

    @Autowired
    private PointOrderMapper pointOrderMapper;
    
    @Autowired
    private UserService userService;

    @Override
    public PointOrder createPointOrder(final String logInfo, PointAccount account, Date nowDate, String period, Integer amount) {
        //根据当前时间和周期计算出结束日期
        Date endDate = DateUtil.getExpiryDate(nowDate, period);
        return createPointOrder(logInfo,account,nowDate,endDate,amount);
    }

    @Override
    public PointOrder createPointOrder(String logInfo, PointAccount account, Date nowDate, Date endDate, Integer amount) {
        logger.info(logInfo+"创建积分订单");

        //查询出是否有结束日期相同的订单，如果结束日期一致则进行订单合并，没有则创建
        PointOrder pointOrder = this.queryPointOrder(account,endDate);
        if(null != pointOrder)
            return this.mergePointOrder(logInfo, pointOrder, nowDate, amount);

        //创建积分订单
        pointOrder = new PointOrder();
        pointOrder.setUserId(account.getUserId());
        pointOrder.setUserMobile(account.getUserMobile());
        pointOrder.setUserName(account.getUserName());
        pointOrder.setAmount(amount);
        pointOrder.setUseableAmount(amount);
        pointOrder.setEndDate(endDate);
        pointOrder.setStatus(Consts.PointOrderStatus.VALID);
        pointOrder.setCreatePer("javaservice");
        pointOrderMapper.insert(pointOrder);

        if(null == pointOrder.getId()){
            logger.info(logInfo+"创建积分订单失败");
            return null;
        }

        logger.info(logInfo+"创建积分订单成功");
        return pointOrder;
    }

    @Override
    public PointOrder queryPointOrder(PointAccount account, Date endDate) {
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("userId",account.getUserId());
        params.put("endDate",endDate);
        params.put("status",Consts.PointOrderStatus.VALID);
        return pointOrderMapper.selectPointOrder(params);
    }

    @Override
    public PointOrder mergePointOrder(final String logInfo, PointOrder pointOrder, Date nowDate, Integer amount) {
        logger.info(logInfo+"合并积分订单");
        if(null == amount || 0 == amount)
            return pointOrder;
        pointOrder.setUpdatePer("javaservice");

        Map<String,Object> params = new HashMap<String, Object>();
        params.put("updatePer","javaservice");
        params.put("amount",amount);
        params.put("useableAmount",amount);
        params.put("id",pointOrder.getId());
        pointOrderMapper.mergePointOrder(params);
        logger.info(logInfo+"合并积分订单成功");
        return pointOrder;
    }

}
